package com.rimi.service.impl;

import com.rimi.bean.Doctor;
import com.rimi.bean.User;
import com.rimi.commons.CommonResult;
import com.rimi.commons.ResultStatus;
import com.rimi.mapper.UserMapper;
import com.rimi.service.UserService;
import com.rimi.util.PwdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author liHaiYang
 * @version V1.0
 * @Description: (用一句话描述该文件做什么)
 * @date 2021/6/28 16:26
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    @Override
    public User findUserByPhoneNumber(String phonenumber) {

        return userMapper.findUserByPhoneNumber(phonenumber);
    }

    @Override
    public User findUserByUserId(Integer id) {
        return userMapper.findUserByUserId(id);
    }

    @Override
    public CommonResult addUerToSql(User user) {
        if(StringUtils.hasText(user.getUserPassword())) {
            String encode = PwdUtil.encode(user.getUserPassword());
            user.setUserPassword(encode);
        }
        int row =  userMapper.addUserToSql(user);
        return row==1?CommonResult.onSuccess(null):CommonResult.onFailed(ResultStatus.ADD_FAILED);
    }

    /**
     * 显示用户信息
     * @return
     */
    @Override
    public List<User> showUser() {
        return userMapper.showUser();
    }

    @Override
    public CommonResult updateUser(User user) {
        if(StringUtils.hasText(user.getUserPassword())) {
            String encode = PwdUtil.encode(user.getUserPassword());
            user.setUserPassword(encode);
        }
        int row = userMapper.updateUser(user);
        return row == 1 ? CommonResult.onSuccess(null):CommonResult.onFailed(ResultStatus.UPDATE_FAILED);
    }

    @Override
    public int deleteUserByUserId(Integer userId) {
        return  userMapper.deleteUserByUserId(userId);
    }

    @Override
    public CommonResult updateUserPasswordByUserId(Integer userId, String newPassword) {
        if(newPassword != null){
            int row = userMapper.updateUserPasswordByUserId(userId,newPassword);
            return row == 1 ? CommonResult.onSuccess(null):CommonResult.onFailed(ResultStatus.UPDATE_FAILED);
        }
        return CommonResult.onFailed(ResultStatus.UPDATE_FAILED);
    }

    @Override
    public int addDoctorByUserId(Integer doctorId, Integer userId) {
        return userMapper.addDoctorByUserId(doctorId,userId);
    }

    @Override
    public List<Doctor> showDoctorByUserId(Integer userId) {
      return userMapper.showDoctorByUserId(userId);
    }

    @Override
    public CommonResult deleteDoctorBydoctorId(Integer doctorId) {
        int row = userMapper.deleteDoctorBydoctorId(doctorId);
        return row == 1?CommonResult.onSuccess(null):CommonResult.onFailed(ResultStatus.DELETE_FAILED);
    }

    @Override
    public int upLoadImg(String filename,Integer userId) {
        return userMapper.upLoadImg(filename,userId);
    }

    @Override
    public CommonResult registerUer(User user) {
        if(StringUtils.hasText(user.getUserPassword())) {
            String encode = PwdUtil.encode(user.getUserPassword());
            user.setUserPassword(encode);
        }
        int row = userMapper.registerUer(user);
        return row==1?CommonResult.onSuccess(null):CommonResult.onFailed(ResultStatus.ADD_FAILED);
    }


}





